"""
@This file will be used to create training and testing database for question migration resercah
"""
import MySQLdb

#"""
site_name = "serverfault"
port=3306
user="root"
password="123"
database="isec2014"
table_migrated = "train_migrated_"+site_name+"_aug13"
table_not_migrated = "train_not_migrated_"+site_name+"_aug13"
"""
site_name = "serverfault"
port=3307
user="sangeetal"
password="sangeetal"
database="sangeeta"
table_migrated = "train_migrated_"+site_name+"_aug13"
table_not_migrated = "train_not_migrated_"+site_name+"_aug13"
#"""

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
select_cursor = db1.cursor()
temp_cursor = db1.cursor()
insert_cursor =db1.cursor()

q_str = "select question_id, initial, migrated from qimp_vote_rep where initial = '"+site_name+"'"
print"q str = ", q_str
select_cursor.execute(q_str)

migrated_data =  select_cursor.fetchall()
print "len = ", len(migrated_data)

for val  in migrated_data:
    current_q = val[0]
    initial = val[1]
    migrated_to = val[2]
    ##migrated = 'meta.askubuntu' # @comment just to run pro
    print "current q =", current_q, initial, migrated_to
    temp_str = "select id, title, body from `posts_"+migrated_to+"_aug13` where id="+(str)(current_q)
    #print "temp str=", temp_str
    temp_cursor.execute(temp_str)
    q_data = temp_cursor.fetchall()
    for temp_q_data in q_data:
        qid = temp_q_data[0]
        f1_title = temp_q_data[1]
        f1_body = temp_q_data[2]
        #print "qid=", qid, "title=", title, "body=", body
        insert_str = "insert into `"+table_migrated+"`  values("+(str)(qid)+",'"+migrated_to+"','"+f1_title+"','"+f1_body+"',"+ (str)(1)+")"
        insert_cursor.execute(insert_str)
        
    
db1.commit()
 


#fill not migrated questions detail in train_not_migrated table
q_str2 = "select id, title, body from `posts_"+site_name+"_aug13`   where posttypeid = 1 and accepted_answer_id!=-1"

temp_cursor.execute(q_str2)

q_not_migrated_data =  temp_cursor.fetchall()
for temp_q_data in q_not_migrated_data:
    qid = temp_q_data[0]
    f1_title = temp_q_data[1]
    f1_body =temp_q_data[2]
    migrated_to='null'
    insert_str = "insert into `"+table_not_migrated+"`  values("+(str)(qid)+",'"+migrated_to+"','"+f1_title+"','"+f1_body+"',"+ (str)(0)+")"
    insert_cursor.execute(insert_str)
        
    
    

db1.commit()



